package com.example.user.customviewdemo.removeitemlistview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by user on 2016/5/27.
 */
public class ExtendLayout extends ViewGroup {

    public ExtendLayout(Context context) {
        super(context);
    }

    public ExtendLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExtendLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = createDeleteButton(context);
        this.addView(view,1);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View deleteView = getChildAt(1);
        int deleteWidth = deleteView.getMeasuredWidth();
        int deleteHeight = deleteView.getMeasuredHeight();
        getChildAt(0).layout(0,0,getMeasuredWidth(),getMeasuredHeight());
        deleteView.layout(getMeasuredWidth(),0,getMeasuredWidth()+deleteWidth,deleteHeight);
    }

    /**
     * 得到删除按钮
     * @return
     */
    public View getDeleteButton(){
        if(getChildCount()==2) {
            return getChildAt(1);
        }else {
            throw new IndexOutOfBoundsException("there is not delete button!");
        }
    }

    /**
     * 得到删除按钮
     * @param context
     * @return
     */
    public View createDeleteButton(Context context){
        TextView view = new TextView(context);
        ViewGroup.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);
        view.setText("delete");
        view.setBackgroundColor(Color.RED);
        view.setGravity(Gravity.CENTER);
        view.setTextSize(dp2px(15));
        view.setClickable(true);

        return view;
    }

    public float dp2px(int value){
       return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,getContext()
               .getResources()
               .getDisplayMetrics());
    }

    /**
     * 测量宽度
     * @param widthMeasureSpec
     * @return
     */
    private int measureWidth(int widthMeasureSpec){
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int width = 0;
        int deleteWidth = 0;
        if(mode == MeasureSpec.EXACTLY){
            width = size;
        }else{
            width = getChildAt(0).getMeasuredWidth();
        }
        deleteWidth = getChildAt(1).getMeasuredWidth();

        return width + deleteWidth;
    }

    /**
     * 测量高度,并将删除按钮的高度设置为相同高度
     * @param heightMeasureSpec
     * @return
     */
    private int measureHeight(int heightMeasureSpec){
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;
        if(mode == MeasureSpec.EXACTLY){
            height = size;
        }else {
            height = getChildAt(0).getMeasuredHeight();
        }
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,height);
        getChildAt(1).setLayoutParams(layoutParams);

        return height;
    }


    @Override
    public void addView(View child, int width, int height) {
        super.addView(child, width, height);
        if(getChildCount() > 2){
            throw new IndexOutOfBoundsException("you can't add too much subview than 2 counts");
        }
    }


}
